<%@ page import="java.io.File"%>
<%
	// Provide default values.
	if (session.getAttribute("errors") == null) {
		session.setAttribute("errors", "");
	}

	//Retrieve necessary session attributes.
	String type = (String) session.getAttribute("type");
	String user = (String) session.getAttribute("user");
	String errors = (String) session.getAttribute("errors");

	// Retrieve necessary form parameters.
	String scholarship_id = request.getParameter("scholarship_id");
	if (session.getAttribute("scholarship_id") != null) {
		scholarship_id = (String)session.getAttribute("scholarship_id");
	}
	
	// Clear variables after reading them.
	session.setAttribute("errors", null);
	session.setAttribute("scholarship_id", null);
%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />
<title>Scholar Aide</title>
<script language="javascript" type="text/javascript">
	function redirectClicked(string) {
		var myForm = document.createElement("form");
		myForm.setAttribute('method', "post");
		myForm.setAttribute('action', "dashboard.jsp");
		var data = document.createElement("input");
		data.setAttribute('type', "hidden");
		data.setAttribute('name', "file");
		data.setAttribute('value', string);
		myForm.appendChild(data);
		javascript: myForm.submit();
	}
	function submitClicked() {
		javascript: apply.submit();
	}
</script>
</head>
<body>
	<jsp:include page="/includes/header.jsp" />
	<div id="wrapper">
		<div id="content">
			<table id="controls">
				<tr>
					<td><a href="logout.jsp">Logout</a></td>
				</tr>
				<tr>
					<td><a href="Javascript:void();" onclick="redirectClicked('home.jsp')">Home</a></td>
				</tr>
				<tr>
					<td><a href="Javascript:void();" onclick="redirectClicked('edit_profile.jsp')">Edit Profile</a></td>
				</tr>
				<%
					if (type.equalsIgnoreCase("student")) {
				%>
				<tr>
					<td><a href="Javascript:void();" onclick="redirectClicked('edit_profile.jsp')">View My Applications</a></td>
				</tr>
				<%
					} else if (type.equalsIgnoreCase("reviewer")) {
				%>
				<tr>
					<td><a href="Javascript:void();" onclick="redirectClicked('new_scholarship.jsp')">Create New Scholarship</a></td>
				</tr>
				<tr>
					<td><a href="Javascript:void();" onclick="redirectClicked('list_unfinished_scholarships.jsp')">Finish New Scholarship</a></td>
				</tr>
				<tr>
					<td><a href="Javascript:void();" onclick="redirectClicked('review_applications.jsp')">Review Applications</a></td>
				</tr>
				<%
					}
				%>
			</table>
			<form name="apply" action="validate_apply.jsp" method="post" enctype="multipart/form-data">
				<table>
					<tr>
						<td>
							<div style="color: red; font-weight: bold;"><%=errors%></div>
						</td>
					</tr>
					<tr>
						<td>
							<%
								// Display the selected scholarship.
								java.sql.Connection conn = null;
								try {
									String myJDBCDriver = (String) session.getAttribute("myJDBCDriver");
									String myURL = (String) session.getAttribute("myURL");
									String myUserID = (String) session.getAttribute("myUserID");
									String myPassword = (String) session.getAttribute("myPassword");

									// Connect to the database.
									Class.forName(myJDBCDriver).newInstance();
									java.util.Properties sysprops = System.getProperties();
									sysprops.put("user", myUserID);
									sysprops.put("password", myPassword);
									conn = java.sql.DriverManager.getConnection(myURL, sysprops);

									java.sql.Statement statement = conn.createStatement();
									java.sql.ResultSet rs = statement.executeQuery("SELECT * FROM scholarships WHERE id='" + scholarship_id + "'");
									java.sql.ResultSetMetaData columns = rs.getMetaData();
									while (rs.next()) {
										out.println("<h1>Application for the " + rs.getString("name") + "</h1>");
										if (rs.getInt("finished") == 1) {
											out.println("<table id=\"scholarship\">");
											for (int i = 2; i <= columns.getColumnCount() - 2; i++) {
												out.println("<tr>");
												out.println("<th>" + columns.getColumnName(i) + ":</th>");
												out.println("<td>" + rs.getString(i) + "</td>");
												out.println("</tr>");
											}
											String docPath = application.getRealPath("scholarship_documents") + "\\" + rs.getString("id") + ".doc";
											File file = new File(docPath);
											if (file.exists()) {
												out.println("<tr><th>extra_info</th><td><a href=\"" + "scholarship_documents/" + file.getName()
														+ "\">Download Extra Information</a></td></tr>");
											}
											out.println("</table>");
											out.println("<br />");
										}
									}

									// Get information from the application database.
									rs = statement.executeQuery("SELECT * FROM applications WHERE scholarship_id='" + scholarship_id + "' AND applicant='" + user + "'");
									columns = rs.getMetaData();
									boolean finished = false;
									if (rs.next()) {
										// There exists an old application.
										if (rs.getString("finished").equals("1")) {
											// The application has already been submitted.
											out.println("<h2>You have already completed this application.</h2>");
											finished = true;
										}
									}
									if (!finished) {
										// This is the first time the user is applying to this scholarship.
										out.println("<h3>Editing your profile before your application is reviewed also update the corresponding information in your application but you cannot edit your uploaded file.</h3>");
										out.println("<b>Upload Supplementary Information: </b><input type=\"file\" name=\"document\" /><br/><br/>");
										out.println("<a href=\"Javascript:void();\" class=\"right red_button\" onclick=\"submitClicked()\">Submit</a>");
										out.println("<input type=\"hidden\" name=\"scholarship_id\" value=\"" + scholarship_id + "\" />");
									}
								} catch (Exception e) {
									e.printStackTrace();
									out.print(e.toString());
								} finally {
									try {
										conn.close();
									} catch (Exception ee) {
									}
								}
							%>
						</td>
					</tr>
				</table>
			</form>
		</div>
	</div>
	<jsp:include page="/includes/footer.jsp" />
</body>
</html>